package org.djw.basic.controller;

import org.djw.basic.exception.BusinessException;
import org.djw.basic.service.IVerifyCodeService;
import org.djw.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 发送验证码
 */
@RestController
@RequestMapping("/verifycode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 点击获取手机验证码接口
     * @param params
     * @return
     */
    @RequestMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody Map<String,String> params){
        try {
            verifyCodeService.sendSmsCode(params);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
         catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
     * 获取邮箱验证码
     * @param params
     * @return
     */
    @RequestMapping("/sendEmailCode")
    public AjaxResult sendEmailCode(@RequestBody Map<String,String> params){
        try {
            String email = params.get("email");
            verifyCodeService.sendEmailCode(email);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
         catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }
}
